#include "arrayoperation.h"
#include "QDebug"

ArrayOperation::ArrayOperation() {
    qDebug()<<"ArrayOperation created！";
}

ArrayOperation::~ArrayOperation()
{
    qDebug()<<"ArrayOperation destroyed";
}


// 计算均值
double ArrayOperation::calculateMean(const QVector<double>& data) {
    if (data.isEmpty())  return 0.0; // 如果数据为空，返回 0
    double sum = std::accumulate(data.begin(),  data.end(),  0.0);
    return sum / data.size();
}

// 计算标准差
double ArrayOperation::calculateStdDev(const QVector<double>& data) {
    if (data.isEmpty())  return 0.0; // 如果数据为空，返回 0
    double mean = calculateMean(data);
    double sumSquaredDiff = 0.0;
    for (double value : data) {
        sumSquaredDiff += (value - mean) * (value - mean);
    }
    return std::sqrt(sumSquaredDiff / data.size());
}
double ArrayOperation::ListofSTD(const QVector<double> &arr)
{
    double temp = 0;
    double meantemp = calculateMean(arr);
    for (int i = 0; i<arr.size(); i++)
    {
        temp = (double)(temp + pow(arr.at(i) - meantemp, 2));
    }

    return (double)sqrt(temp / (arr.size() - 1));
}

